package by.msokoloff.javaweb2015.webapp.page.admuser;

import java.util.Iterator;

import javax.inject.Inject;
import javax.persistence.metamodel.SingularAttribute;

import org.apache.wicket.extensions.markup.html.repeater.data.sort.OrderByBorder;
import org.apache.wicket.extensions.markup.html.repeater.data.sort.SortOrder;
import org.apache.wicket.extensions.markup.html.repeater.util.SortableDataProvider;
import org.apache.wicket.markup.html.WebMarkupContainer;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.image.Image;
import org.apache.wicket.markup.html.link.Link;
import org.apache.wicket.markup.html.navigation.paging.PagingNavigator;
import org.apache.wicket.markup.html.panel.Panel;
import org.apache.wicket.markup.repeater.Item;
import org.apache.wicket.markup.repeater.data.DataView;
import org.apache.wicket.model.CompoundPropertyModel;
import org.apache.wicket.model.IModel;
import org.apache.wicket.request.resource.ContextRelativeResource;
import org.apache.wicket.request.resource.IResource;

import by.msokoloff.javaweb2015.datamodel.AdmUser;
import by.msokoloff.javaweb2015.datamodel.AdmUser_;
import by.msokoloff.javaweb2015.services.AdmUserService;
import by.msokoloff.javaweb2015.webapp.page.EntityEditPage;
import by.msokoloff.javaweb2015.webapp.page.EntityListPage;

public class AdmUserListPanel extends Panel {
	
	@Inject
	private AdmUserService admUserService;

	public AdmUserListPanel(String id) {
		super(id);
		
		AdmUserDataProvider provider = new AdmUserDataProvider();
		
		DataView<AdmUser> dv = new DataView<AdmUser>("list", provider, 25) {

			IResource editIconResource = new ContextRelativeResource("img/editIcon.png");
			IResource deleteIconResource = new ContextRelativeResource("img/deleteIcon.png");
			
			@Override
			protected void populateItem(Item<AdmUser> item) {
				AdmUser admUser = item.getModelObject();
				item.add(new Label("id"));
				item.add(new Label("login"));
				item.add(new Label("name"));
				item.add(new Label("email"));
				
				Link<Void> editLink = new Link<Void>("edit") {

					@Override
					public void onClick() {
						setResponsePage(new EntityEditPage(admUser));
						
					}
					
				};
				
				editLink.add(new Image("editicon", editIconResource));
				item.add(editLink);
				
				Link<Void> deleteLink = new Link<Void>("delete") {

					@Override
					public void onClick() {
						admUserService.delete(admUser);
						setResponsePage(new EntityListPage(AdmUser.class));
					}
					
				};
				
				deleteLink.add(new Image("deleteicon", deleteIconResource));
				item.add(deleteLink);
				
			}
		};
		
		WebMarkupContainer tableBody = new WebMarkupContainer("wrapper-body");
		add(tableBody);
		tableBody.add(dv);
		
		add(new PagingNavigator("paging", dv));
		
		add(new OrderByBorder<SingularAttribute<AdmUser, ?>>("sortByLogin", AdmUser_.login, provider));
	
		add(new Link<Void>("create") {

			@Override
			public void onClick() {
				setResponsePage(new EntityEditPage(new AdmUser()));
				
			}
			
		});
	}
	
	private class AdmUserDataProvider extends SortableDataProvider<AdmUser, SingularAttribute<AdmUser, ?>> {

		public AdmUserDataProvider() {
			super();
			setSort(AdmUser_.name, SortOrder.ASCENDING);
		}
		
		@Override
		public Iterator<? extends AdmUser> iterator(long first, long count) {
			SingularAttribute<AdmUser, ?> sortParam = getSort().getProperty();
			SortOrder propertySortOrder = getSortState().getPropertySortOrder(sortParam);
			boolean ascending = SortOrder.ASCENDING.equals(propertySortOrder);
			return admUserService.getAll(sortParam, ascending, (int) first, (int) count).iterator();
		}

		@Override
		public long size() {
			return admUserService.getCount();
		}

		@Override
		public IModel<AdmUser> model(AdmUser object) {
			return new CompoundPropertyModel<AdmUser>(object);
		}
		
	}

}
